<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>DataFrame运算 | 机器学习（常用科学计算库的使用）基础定位、目标</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-katex/katex.min.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-expandable-chapters/expandable-chapters.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../Pandas/section5.html" />
    
    
    <link rel="prev" href="../Pandas/section3.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.4"
        data-chapter-title="DataFrame运算"
        data-filepath="Pandas/section4.md"
        data-basepath=".."
        data-revision="Sat Jul 20 2019 23:53:14 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        机器学习（常用科学计算库的使用）基础定位、目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="ml_pre/index.html">
            
                
                    <a href="../ml_pre/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        机器学习概述
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="ml_pre/section1.html">
            
                
                    <a href="../ml_pre/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        人工智能概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="ml_pre/section2.html">
            
                
                    <a href="../ml_pre/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        人工智能发展历程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="ml_pre/section3.html">
            
                
                    <a href="../ml_pre/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        人工智能主要分支
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="ml_pre/section4.html">
            
                
                    <a href="../ml_pre/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        机器学习工作流程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="ml_pre/section5.html">
            
                
                    <a href="../ml_pre/section5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        机器学习算法分类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="ml_pre/section6.html">
            
                
                    <a href="../ml_pre/section6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        模型评估
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="ml_pre/section7.html">
            
                
                    <a href="../ml_pre/section7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        Azure机器学习模型搭建实验
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="ml_pre/section8.html">
            
                
                    <a href="../ml_pre/section8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        深度学习简介
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="env/index.html">
            
                
                    <a href="../env/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        机器学习基础环境安装与使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="env/section1.html">
            
                
                    <a href="../env/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        库的安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="env/section2.html">
            
                
                    <a href="../env/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        jupyter notebook使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="Matplotlib/index.html">
            
                
                    <a href="../Matplotlib/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Matplotlib
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="Matplotlib/section1.html">
            
                
                    <a href="../Matplotlib/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Matplotlib之HelloWorld
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="Matplotlib/section2.html">
            
                
                    <a href="../Matplotlib/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        基础绘图功能 — 以折线图为例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="Matplotlib/section3.html">
            
                
                    <a href="../Matplotlib/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        常见图形绘制
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="Numpy/index.html">
            
                
                    <a href="../Numpy/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        Numpy
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="Numpy/section1.html">
            
                
                    <a href="../Numpy/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Numpy的优势
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="Numpy/section2.html">
            
                
                    <a href="../Numpy/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        N维数组-ndarray
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="Numpy/section3.html">
            
                
                    <a href="../Numpy/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="Numpy/section4.html">
            
                
                    <a href="../Numpy/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        ndarray运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="Numpy/section5.html">
            
                
                    <a href="../Numpy/section5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        数组间的运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="Numpy/section6.html">
            
                
                    <a href="../Numpy/section6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        数学：矩阵
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="Pandas/index.html">
            
                
                    <a href="../Pandas/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        Pandas
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="Pandas/section1.html">
            
                
                    <a href="../Pandas/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        Pandas介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="Pandas/section2.html">
            
                
                    <a href="../Pandas/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        Pandas数据结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="Pandas/section3.html">
            
                
                    <a href="../Pandas/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        基本数据操作
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.4" data-path="Pandas/section4.html">
            
                
                    <a href="../Pandas/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        DataFrame运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="Pandas/section5.html">
            
                
                    <a href="../Pandas/section5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        Pandas画图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="Pandas/section6.html">
            
                
                    <a href="../Pandas/section6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        文件读取与存储
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="Pandas/section7.html">
            
                
                    <a href="../Pandas/section7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        高级处理-缺失值处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="Pandas/section8.html">
            
                
                    <a href="../Pandas/section8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        高级处理-数据离散化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="Pandas/section9.html">
            
                
                    <a href="../Pandas/section9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        高级处理-合并
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.10" data-path="Pandas/section10.html">
            
                
                    <a href="../Pandas/section10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.10.</b>
                        
                        高级处理-交叉表与透视表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.11" data-path="Pandas/section11.html">
            
                
                    <a href="../Pandas/section11.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.11.</b>
                        
                        高级处理-分组与聚合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.12" data-path="Pandas/section12.html">
            
                
                    <a href="../Pandas/section12.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.12.</b>
                        
                        案例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="ReadingExtension/index.html">
            
                
                    <a href="../ReadingExtension/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        拓展知识
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="ReadingExtension/1.完整机器学习项目的流程.html">
            
                
                    <a href="../ReadingExtension/1.完整机器学习项目的流程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        完整机器学习项目的流程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="ReadingExtension/2.独立同分布.html">
            
                
                    <a href="../ReadingExtension/2.独立同分布.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        独立同分布
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >机器学习（常用科学计算库的使用）基础定位、目标</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="54-dataframe&#x8FD0;&#x7B97;">5.4 DataFrame&#x8FD0;&#x7B97;</h1>
<h2 id="&#x5B66;&#x4E60;&#x76EE;&#x6807;">&#x5B66;&#x4E60;&#x76EE;&#x6807;</h2>
<ul>
<li>&#x76EE;&#x6807;<ul>
<li>&#x5E94;&#x7528;add&#x7B49;&#x5B9E;&#x73B0;&#x6570;&#x636E;&#x95F4;&#x7684;&#x52A0;&#x3001;&#x51CF;&#x6CD5;&#x8FD0;&#x7B97;</li>
<li>&#x5E94;&#x7528;&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;&#x53F7;&#x5B9E;&#x73B0;&#x6570;&#x636E;&#x7684;&#x903B;&#x8F91;&#x7B5B;&#x9009;</li>
<li>&#x5E94;&#x7528;isin, query&#x5B9E;&#x73B0;&#x6570;&#x636E;&#x7684;&#x7B5B;&#x9009;</li>
<li>&#x4F7F;&#x7528;describe&#x5B8C;&#x6210;&#x7EFC;&#x5408;&#x7EDF;&#x8BA1;</li>
<li>&#x4F7F;&#x7528;max, min, mean, std&#x5B8C;&#x6210;&#x7EDF;&#x8BA1;&#x8BA1;&#x7B97;</li>
<li>&#x4F7F;&#x7528;idxmin&#x3001;idxmax&#x5B8C;&#x6210;&#x6700;&#x5927;&#x503C;&#x6700;&#x5C0F;&#x503C;&#x7684;&#x7D22;&#x5F15;</li>
<li>&#x4F7F;&#x7528;cumsum&#x7B49;&#x5B9E;&#x73B0;&#x7D2F;&#x8BA1;&#x5206;&#x6790;</li>
<li>&#x5E94;&#x7528;apply&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x6570;&#x636E;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x5904;&#x7406;</li>
</ul>
</li>
</ul>
<hr>
<h2 id="1-&#x7B97;&#x672F;&#x8FD0;&#x7B97;">1 &#x7B97;&#x672F;&#x8FD0;&#x7B97;</h2>
<ul>
<li>add(other)</li>
</ul>
<p>&#x6BD4;&#x5982;&#x8FDB;&#x884C;&#x6570;&#x5B66;&#x8FD0;&#x7B97;&#x52A0;&#x4E0A;&#x5177;&#x4F53;&#x7684;&#x4E00;&#x4E2A;&#x6570;&#x5B57;</p>
<pre><code class="lang-python">data[<span class="hljs-string">&apos;open&apos;</span>].add(<span class="hljs-number">1</span>)

<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">27</span>    <span class="hljs-number">24.53</span>
<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">26</span>    <span class="hljs-number">23.80</span>
<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">23</span>    <span class="hljs-number">23.88</span>
<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">22</span>    <span class="hljs-number">23.25</span>
<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">14</span>    <span class="hljs-number">22.49</span>
</code></pre>
<ul>
<li>sub(other)&apos;</li>
</ul>
<h2 id="2-&#x903B;&#x8F91;&#x8FD0;&#x7B97;">2 &#x903B;&#x8F91;&#x8FD0;&#x7B97;</h2>
<h3 id="21-&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;&#x53F7;">2.1 &#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;&#x53F7;</h3>
<ul>
<li>&#x4F8B;&#x5982;&#x7B5B;&#x9009;data[&quot;open&quot;] &gt; 23&#x7684;&#x65E5;&#x671F;&#x6570;&#x636E;<ul>
<li>data[&quot;open&quot;] &gt; 23&#x8FD4;&#x56DE;&#x903B;&#x8F91;&#x7ED3;&#x679C;</li>
</ul>
</li>
</ul>
<pre><code class="lang-python">data[<span class="hljs-string">&quot;open&quot;</span>] &gt; <span class="hljs-number">23</span>

<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">27</span>     <span class="hljs-keyword">True</span>
<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">26</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">23</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">22</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">2018</span>-<span class="hljs-number">02</span>-<span class="hljs-number">14</span>    <span class="hljs-keyword">False</span>
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># &#x903B;&#x8F91;&#x5224;&#x65AD;&#x7684;&#x7ED3;&#x679C;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x7B5B;&#x9009;&#x7684;&#x4F9D;&#x636E;</span>
data[data[<span class="hljs-string">&quot;open&quot;</span>] &gt; <span class="hljs-number">23</span>].head()
</code></pre>
<p><img src="images/&#x903B;&#x8F91;&#x4E3E;&#x4F8B;1.png" alt="image-20190624115656264"></p>
<ul>
<li>&#x5B8C;&#x6210;&#x591A;&#x4E2A;&#x903B;&#x8F91;&#x5224;&#x65AD;&#xFF0C;</li>
</ul>
<pre><code class="lang-python">data[(data[<span class="hljs-string">&quot;open&quot;</span>] &gt; <span class="hljs-number">23</span>) &amp; (data[<span class="hljs-string">&quot;open&quot;</span>] &lt; <span class="hljs-number">24</span>)].head()
</code></pre>
<p><img src="images/&#x903B;&#x8F91;&#x4E3E;&#x4F8B;2.png" alt="image-20190624115753590"></p>
<h3 id="22--&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x51FD;&#x6570;">2.2  &#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x51FD;&#x6570;</h3>
<ul>
<li>query(expr)<ul>
<li>expr:&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;</li>
</ul>
</li>
</ul>
<p>&#x901A;&#x8FC7;query&#x4F7F;&#x5F97;&#x521A;&#x624D;&#x7684;&#x8FC7;&#x7A0B;&#x66F4;&#x52A0;&#x65B9;&#x4FBF;&#x7B80;&#x5355;</p>
<pre><code class="lang-python">data.query(<span class="hljs-string">&quot;open&lt;24 &amp; open&gt;23&quot;</span>).head()
</code></pre>
<ul>
<li>isin(values)</li>
</ul>
<p>&#x4F8B;&#x5982;&#x5224;&#x65AD;&apos;open&apos;&#x662F;&#x5426;&#x4E3A;23.53&#x548C;23.85</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x503C;&#x8FDB;&#x884C;&#x4E00;&#x4E2A;&#x5224;&#x65AD;&#xFF0C;&#x4ECE;&#x800C;&#x8FDB;&#x884C;&#x7B5B;&#x9009;&#x64CD;&#x4F5C;</span>
data[data[<span class="hljs-string">&quot;open&quot;</span>].isin([<span class="hljs-number">23.53</span>, <span class="hljs-number">23.85</span>])]
</code></pre>
<p><img src="images/isin.png" alt="image-20190624115947522"></p>
<h2 id="3-&#x7EDF;&#x8BA1;&#x8FD0;&#x7B97;">3 &#x7EDF;&#x8BA1;&#x8FD0;&#x7B97;</h2>
<h3 id="31-describe">3.1 describe</h3>
<p>&#x7EFC;&#x5408;&#x5206;&#x6790;: &#x80FD;&#x591F;&#x76F4;&#x63A5;&#x5F97;&#x51FA;&#x5F88;&#x591A;&#x7EDF;&#x8BA1;&#x7ED3;&#x679C;,<code>count</code>, <code>mean</code>, <code>std</code>, <code>min</code>, <code>max</code> &#x7B49;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x8BA1;&#x7B97;&#x5E73;&#x5747;&#x503C;&#x3001;&#x6807;&#x51C6;&#x5DEE;&#x3001;&#x6700;&#x5927;&#x503C;&#x3001;&#x6700;&#x5C0F;&#x503C;</span>
data.describe()
</code></pre>
<p><img src="images/describe&#x7ED3;&#x679C;.png" alt="describe&#x7ED3;&#x679C;"></p>
<h3 id="32-&#x7EDF;&#x8BA1;&#x51FD;&#x6570;">3.2 &#x7EDF;&#x8BA1;&#x51FD;&#x6570;</h3>
<p>Numpy&#x5F53;&#x4E2D;&#x5DF2;&#x7ECF;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x6F14;&#x793A;min(&#x6700;&#x5C0F;&#x503C;), max(&#x6700;&#x5927;&#x503C;), mean(&#x5E73;&#x5747;&#x503C;), median(&#x4E2D;&#x4F4D;&#x6570;), var(&#x65B9;&#x5DEE;), std(&#x6807;&#x51C6;&#x5DEE;),mode(&#x4F17;&#x6570;)&#x7ED3;&#x679C;:</p>
<table>
<thead>
<tr>
<th><code>count</code></th>
<th>Number of non-NA observations</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>sum</code></td>
<td><strong>Sum of values</strong></td>
</tr>
<tr>
<td><code>mean</code></td>
<td><strong>Mean of values</strong></td>
</tr>
<tr>
<td><code>median</code></td>
<td>Arithmetic median of values</td>
</tr>
<tr>
<td><code>min</code></td>
<td><strong>Minimum</strong></td>
</tr>
<tr>
<td><code>max</code></td>
<td><strong>Maximum</strong></td>
</tr>
<tr>
<td><code>mode</code></td>
<td>Mode</td>
</tr>
<tr>
<td><code>abs</code></td>
<td>Absolute Value</td>
</tr>
<tr>
<td><code>prod</code></td>
<td>Product of values</td>
</tr>
<tr>
<td><code>std</code></td>
<td><strong>Bessel-corrected sample standard deviation</strong></td>
</tr>
<tr>
<td><code>var</code></td>
<td><strong>Unbiased variance</strong></td>
</tr>
<tr>
<td><code>idxmax</code></td>
<td>compute the index labels with the maximum</td>
</tr>
<tr>
<td><code>idxmin</code></td>
<td>compute the index labels with the minimum</td>
</tr>
</tbody>
</table>
<p><strong>&#x5BF9;&#x4E8E;&#x5355;&#x4E2A;&#x51FD;&#x6570;&#x53BB;&#x8FDB;&#x884C;&#x7EDF;&#x8BA1;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5750;&#x6807;&#x8F74;&#x8FD8;&#x662F;&#x6309;&#x7167;&#x9ED8;&#x8BA4;&#x5217;&#x201C;columns&#x201D; (axis=0, default)&#xFF0C;&#x5982;&#x679C;&#x8981;&#x5BF9;&#x884C;&#x201C;index&#x201D; &#x9700;&#x8981;&#x6307;&#x5B9A;(axis=1)</strong></p>
<ul>
<li>max()&#x3001;min()</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4F7F;&#x7528;&#x7EDF;&#x8BA1;&#x51FD;&#x6570;&#xFF1A;0 &#x4EE3;&#x8868;&#x5217;&#x6C42;&#x7ED3;&#x679C;&#xFF0C; 1 &#x4EE3;&#x8868;&#x884C;&#x6C42;&#x7EDF;&#x8BA1;&#x7ED3;&#x679C;</span>
data.max(<span class="hljs-number">0</span>)

open                   <span class="hljs-number">34.99</span>
high                   <span class="hljs-number">36.35</span>
close                  <span class="hljs-number">35.21</span>
low                    <span class="hljs-number">34.01</span>
volume             <span class="hljs-number">501915.41</span>
price_change            <span class="hljs-number">3.03</span>
p_change               <span class="hljs-number">10.03</span>
turnover               <span class="hljs-number">12.56</span>
my_price_change         <span class="hljs-number">3.41</span>
dtype: float64
</code></pre>
<ul>
<li>std()&#x3001;var()</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x65B9;&#x5DEE;</span>
data.var(<span class="hljs-number">0</span>)

open               <span class="hljs-number">1.545255e+01</span>
high               <span class="hljs-number">1.662665e+01</span>
close              <span class="hljs-number">1.554572e+01</span>
low                <span class="hljs-number">1.437902e+01</span>
volume             <span class="hljs-number">5.458124e+09</span>
price_change       <span class="hljs-number">8.072595e-01</span>
p_change           <span class="hljs-number">1.664394e+01</span>
turnover           <span class="hljs-number">4.323800e+00</span>
my_price_change    <span class="hljs-number">6.409037e-01</span>
dtype: float64

<span class="hljs-comment"># &#x6807;&#x51C6;&#x5DEE;</span>
data.std(<span class="hljs-number">0</span>)

open                   <span class="hljs-number">3.930973</span>
high                   <span class="hljs-number">4.077578</span>
close                  <span class="hljs-number">3.942806</span>
low                    <span class="hljs-number">3.791968</span>
volume             <span class="hljs-number">73879.119354</span>
price_change           <span class="hljs-number">0.898476</span>
p_change               <span class="hljs-number">4.079698</span>
turnover               <span class="hljs-number">2.079375</span>
my_price_change        <span class="hljs-number">0.800565</span>
dtype: float64
</code></pre>
<ul>
<li><strong>median()&#xFF1A;&#x4E2D;&#x4F4D;&#x6570;</strong></li>
</ul>
<p>&#x4E2D;&#x4F4D;&#x6570;&#x4E3A;&#x5C06;&#x6570;&#x636E;&#x4ECE;&#x5C0F;&#x5230;&#x5927;&#x6392;&#x5217;&#xFF0C;&#x5728;&#x6700;&#x4E2D;&#x95F4;&#x7684;&#x90A3;&#x4E2A;&#x6570;&#x4E3A;&#x4E2D;&#x4F4D;&#x6570;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4E2D;&#x95F4;&#x6570;&#xFF0C;&#x53D6;&#x4E2D;&#x95F4;&#x4E24;&#x4E2A;&#x6570;&#x7684;&#x5E73;&#x5747;&#x503C;&#x3002;</p>
<pre><code class="lang-python">df = pd.DataFrame({<span class="hljs-string">&apos;COL1&apos;</span> : [<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">4</span>,<span class="hljs-number">2</span>],
                   <span class="hljs-string">&apos;COL2&apos;</span> : [<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">2</span>]})

df.median()

COL1    <span class="hljs-number">3.5</span>
COL2    <span class="hljs-number">2.0</span>
dtype: float64
</code></pre>
<ul>
<li>idxmax()&#x3001;idxmin()</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x6C42;&#x51FA;&#x6700;&#x5927;&#x503C;&#x7684;&#x4F4D;&#x7F6E;</span>
data.idxmax(axis=<span class="hljs-number">0</span>)

open               <span class="hljs-number">2015</span>-<span class="hljs-number">06</span>-<span class="hljs-number">15</span>
high               <span class="hljs-number">2015</span>-<span class="hljs-number">06</span>-<span class="hljs-number">10</span>
close              <span class="hljs-number">2015</span>-<span class="hljs-number">06</span>-<span class="hljs-number">12</span>
low                <span class="hljs-number">2015</span>-<span class="hljs-number">06</span>-<span class="hljs-number">12</span>
volume             <span class="hljs-number">2017</span>-<span class="hljs-number">10</span>-<span class="hljs-number">26</span>
price_change       <span class="hljs-number">2015</span>-<span class="hljs-number">06</span>-<span class="hljs-number">09</span>
p_change           <span class="hljs-number">2015</span>-<span class="hljs-number">08</span>-<span class="hljs-number">28</span>
turnover           <span class="hljs-number">2017</span>-<span class="hljs-number">10</span>-<span class="hljs-number">26</span>
my_price_change    <span class="hljs-number">2015</span>-<span class="hljs-number">07</span>-<span class="hljs-number">10</span>
dtype: object


<span class="hljs-comment"># &#x6C42;&#x51FA;&#x6700;&#x5C0F;&#x503C;&#x7684;&#x4F4D;&#x7F6E;</span>
data.idxmin(axis=<span class="hljs-number">0</span>)

open               <span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">02</span>
high               <span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">02</span>
close              <span class="hljs-number">2015</span>-<span class="hljs-number">09</span>-<span class="hljs-number">02</span>
low                <span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">02</span>
volume             <span class="hljs-number">2016</span>-<span class="hljs-number">07</span>-<span class="hljs-number">06</span>
price_change       <span class="hljs-number">2015</span>-<span class="hljs-number">06</span>-<span class="hljs-number">15</span>
p_change           <span class="hljs-number">2015</span>-<span class="hljs-number">09</span>-<span class="hljs-number">01</span>
turnover           <span class="hljs-number">2016</span>-<span class="hljs-number">07</span>-<span class="hljs-number">06</span>
my_price_change    <span class="hljs-number">2015</span>-<span class="hljs-number">06</span>-<span class="hljs-number">15</span>
dtype: object
</code></pre>
<h3 id="33-&#x7D2F;&#x8BA1;&#x7EDF;&#x8BA1;&#x51FD;&#x6570;">3.3 &#x7D2F;&#x8BA1;&#x7EDF;&#x8BA1;&#x51FD;&#x6570;</h3>
<table>
<thead>
<tr>
<th>&#x51FD;&#x6570;</th>
<th>&#x4F5C;&#x7528;</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>cumsum</code></td>
<td><strong>&#x8BA1;&#x7B97;&#x524D;1/2/3/&#x2026;/n&#x4E2A;&#x6570;&#x7684;&#x548C;</strong></td>
</tr>
<tr>
<td><code>cummax</code></td>
<td>&#x8BA1;&#x7B97;&#x524D;1/2/3/&#x2026;/n&#x4E2A;&#x6570;&#x7684;&#x6700;&#x5927;&#x503C;</td>
</tr>
<tr>
<td><code>cummin</code></td>
<td>&#x8BA1;&#x7B97;&#x524D;1/2/3/&#x2026;/n&#x4E2A;&#x6570;&#x7684;&#x6700;&#x5C0F;&#x503C;</td>
</tr>
<tr>
<td><code>cumprod</code></td>
<td>&#x8BA1;&#x7B97;&#x524D;1/2/3/&#x2026;/n&#x4E2A;&#x6570;&#x7684;&#x79EF;</td>
</tr>
</tbody>
</table>
<p><strong>&#x90A3;&#x4E48;&#x8FD9;&#x4E9B;&#x7D2F;&#x8BA1;&#x7EDF;&#x8BA1;&#x51FD;&#x6570;&#x600E;&#x4E48;&#x7528;&#xFF1F;</strong></p>
<p><img src="images/cumsum1.png" alt="cumsum1"></p>
<p>&#x4EE5;&#x4E0A;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5BF9;series&#x548C;dataframe&#x64CD;&#x4F5C;</p>
<p>&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x6309;&#x7167;&#x65F6;&#x95F4;&#x7684;&#x4ECE;&#x524D;&#x5F80;&#x540E;&#x6765;&#x8FDB;&#x884C;&#x7D2F;&#x8BA1;</p>
<ul>
<li>&#x6392;&#x5E8F;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x6392;&#x5E8F;&#x4E4B;&#x540E;&#xFF0C;&#x8FDB;&#x884C;&#x7D2F;&#x8BA1;&#x6C42;&#x548C;</span>
data = data.sort_index()
</code></pre>
<ul>
<li>&#x5BF9;p_change&#x8FDB;&#x884C;&#x6C42;&#x548C;</li>
</ul>
<pre><code class="lang-python">stock_rise = data[<span class="hljs-string">&apos;p_change&apos;</span>]
<span class="hljs-comment"># plot&#x65B9;&#x6CD5;&#x96C6;&#x6210;&#x4E86;&#x524D;&#x9762;&#x76F4;&#x65B9;&#x56FE;&#x3001;&#x6761;&#x5F62;&#x56FE;&#x3001;&#x997C;&#x56FE;&#x3001;&#x6298;&#x7EBF;&#x56FE;</span>
stock_rise.cumsum()

<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">02</span>      <span class="hljs-number">2.62</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">03</span>      <span class="hljs-number">4.06</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">04</span>      <span class="hljs-number">5.63</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">05</span>      <span class="hljs-number">7.65</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">06</span>     <span class="hljs-number">16.16</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">09</span>     <span class="hljs-number">16.37</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">10</span>     <span class="hljs-number">18.75</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">11</span>     <span class="hljs-number">16.36</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">12</span>     <span class="hljs-number">15.03</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">13</span>     <span class="hljs-number">17.58</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">16</span>     <span class="hljs-number">20.34</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">17</span>     <span class="hljs-number">22.42</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">18</span>     <span class="hljs-number">23.28</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">19</span>     <span class="hljs-number">23.74</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">20</span>     <span class="hljs-number">23.48</span>
<span class="hljs-number">2015</span>-<span class="hljs-number">03</span>-<span class="hljs-number">23</span>     <span class="hljs-number">23.74</span>
</code></pre>
<p><strong>&#x90A3;&#x4E48;&#x5982;&#x4F55;&#x8BA9;&#x8FD9;&#x4E2A;&#x8FDE;&#x7EED;&#x6C42;&#x548C;&#x7684;&#x7ED3;&#x679C;&#x66F4;&#x597D;&#x7684;&#x663E;&#x793A;&#x5462;&#xFF1F;</strong></p>
<p><img src="images/cumsum.png" alt="cumsum"></p>
<p>&#x5982;&#x679C;&#x8981;&#x4F7F;&#x7528;plot&#x51FD;&#x6570;&#xFF0C;&#x9700;&#x8981;&#x5BFC;&#x5165;matplotlib.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-comment"># plot&#x663E;&#x793A;&#x56FE;&#x5F62;</span>
stock_rise.cumsum().plot()
<span class="hljs-comment"># &#x9700;&#x8981;&#x8C03;&#x7528;show&#xFF0C;&#x624D;&#x80FD;&#x663E;&#x793A;&#x51FA;&#x7ED3;&#x679C;</span>
plt.show()
</code></pre>
<blockquote>
<p>&#x5173;&#x4E8E;plot&#xFF0C;&#x7A0D;&#x540E;&#x4F1A;&#x4ECB;&#x7ECD;API&#x7684;&#x9009;&#x62E9;</p>
</blockquote>
<h2 id="4-&#x81EA;&#x5B9A;&#x4E49;&#x8FD0;&#x7B97;">4 &#x81EA;&#x5B9A;&#x4E49;&#x8FD0;&#x7B97;</h2>
<ul>
<li>apply(func, axis=0)<ul>
<li>func:&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;</li>
<li>axis=0:&#x9ED8;&#x8BA4;&#x662F;&#x5217;&#xFF0C;axis=1&#x4E3A;&#x884C;&#x8FDB;&#x884C;&#x8FD0;&#x7B97;</li>
</ul>
</li>
<li>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x5BF9;&#x5217;&#xFF0C;&#x6700;&#x5927;&#x503C;-&#x6700;&#x5C0F;&#x503C;&#x7684;&#x51FD;&#x6570;</li>
</ul>
<pre><code class="lang-python">data[[<span class="hljs-string">&apos;open&apos;</span>, <span class="hljs-string">&apos;close&apos;</span>]].apply(<span class="hljs-keyword">lambda</span> x: x.max() - x.min(), axis=<span class="hljs-number">0</span>)

open     <span class="hljs-number">22.74</span>
close    <span class="hljs-number">22.85</span>
dtype: float64
</code></pre>
<h2 id="5-&#x5C0F;&#x7ED3;">5 &#x5C0F;&#x7ED3;</h2>
<ul>
<li>&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#x3010;&#x77E5;&#x9053;&#x3011;</li>
<li>&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x3010;&#x77E5;&#x9053;&#x3011;<ul>
<li>1.&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;&#x53F7;</li>
<li>2.&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x51FD;&#x6570;<ul>
<li>&#x5BF9;&#x8C61;.query()</li>
<li>&#x5BF9;&#x8C61;.isin()</li>
</ul>
</li>
</ul>
</li>
<li>&#x7EDF;&#x8BA1;&#x8FD0;&#x7B97;&#x3010;&#x77E5;&#x9053;&#x3011;<ul>
<li>1.&#x5BF9;&#x8C61;.describe()</li>
<li>2.&#x7EDF;&#x8BA1;&#x51FD;&#x6570;</li>
<li>3.&#x7D2F;&#x79EF;&#x7EDF;&#x8BA1;&#x51FD;&#x6570;</li>
</ul>
</li>
<li>&#x81EA;&#x5B9A;&#x4E49;&#x8FD0;&#x7B97;&#x3010;&#x77E5;&#x9053;&#x3011;<ul>
<li>apply(func, axis=0)</li>
</ul>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../Pandas/section3.html" class="navigation navigation-prev " aria-label="Previous page: 基本数据操作"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../Pandas/section5.html" class="navigation navigation-next " aria-label="Next page: Pandas画图"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"katex":{},"expandable-chapters":{},"splitter":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
